package com.app.service.impl;

import com.app.bean.News;
import com.app.bean.NewsExample;
import com.app.dao.NewsMapper;
import com.app.service.NewsService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/21 20:06
 * @Description:
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;


    @Override
    public List<News> list(String title) {
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        if (StringUtilsEx.isNotEmpty(title)) {
            criteria.andTitleEqualTo("%" + title + "%");
        }
        List<News> newsList = newsMapper.selectByExample(newsExample);
        return newsList;
    }

    /**
     * 获取最新的三条新闻
     * @return
     */
    @Override
    public List<News> newListThree() {
        NewsExample newsExample = new NewsExample();
        newsExample.setOrderByClause("publicdate desc");
        NewsExample.Criteria criteria = newsExample.createCriteria();
        PageHelper.startPage(1,3);
        List<News> newsList = newsMapper.selectByExample(newsExample);
        return newsList;
    }

    @Override
    public News getById(String id) {
        return newsMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public int add(News news) {
        news.setPublicdate(new Date());
        return newsMapper.insert(news);
    }

    @Override
    public int update(News news) {
        return newsMapper.updateByPrimaryKey(news);
    }


    @Override
    public int delete(String id) {
        return newsMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }


}
